<?php

############################################
##          Code by Vu Quang Tam          ##
##       Email: quangtam.info@gmail.com   ##
##       Website: http://quangtam.info    ##
##                http://wapmaster.vn     ##
##     SDT: 0916 097 381 - 0982 655 098   ##
##             May 24, 2011               ##
############################################
date_default_timezone_set('Asia/Ho_Chi_Minh');


include('config.php');
$phone = $_REQUEST['p'];
$mess = $_REQUEST['m'];
$mo_id = $_REQUEST['mo'];

$ex = explode(" ", $mess);
$cuphap = $ex[0] . " ";
$text = str_replace($cuphap, "", $mess);
$text = strtolower($text);


$mysql = new mysql();
$mysql->connec();




$mysql->query("INSERT INTO " . SpeedSMSConfig::$TB_FIX . "log (`phone`,`mess`,`mo_id`) VALUES('$phone','$mess','$mo_id')");

$q = $mysql->query("SELECT id,win,time FROM " . SpeedSMSConfig::$TB_FIX . "text WHERE text='$text'");
$rs = $mysql->fetch_array($q);
$timenow = time();
$timeBegin = strtotime($rs['time']);
# $timenow."<br/>".$timeBegin."<br/>";

$run=$timenow-$timeBegin;
#echo $run."<br />";
#echo $rs['time']; // Debug only

if ($rs['id']) {
    # Luu Log SMS hop le
    $mysql->query("INSERT INTO " . SpeedSMSConfig::$TB_FIX . "data (`phone`,`mess`) VALUES('$phone','$mess')");
    $point = SpeedSMSConfig::$TIME_GAME - $run;

    # Check time chuong trinh,mac dinh la 160s (Config trong file config.php)
    if ($point > 0 && $point < SpeedSMSConfig::$TIME_GAME) {

        # Neu da co nguoi WIN

        if ($rs['win']) {

            # Lay ra id phone tu phone_id
            $iq = $mysql->query("SELECT id FROM " . SpeedSMSConfig::$TB_FIX . "data WHERE phone='$phone'");
            $irs = $mysql->fetch_array($iq);

            # Check xem phone da co trong bang point hay chua
            $qrs = $mysql->query("SELECT * FROM " . SpeedSMSConfig::$TB_FIX . "point WHERE id='$irs[id]'");
            $prs = $mysql->fetch_array($qrs);

            # Neu co thi Update + point

            if ($prs['id']) {
                $mysql->query("UPDATE " . SpeedSMSConfig::$TB_FIX . "point SET day_1=day_1+$point WHERE id='$prs[id]'");
            }
            # Chu co thi Insert moi
            else {
                $mysql->query("INSERT " . SpeedSMSConfig::$TB_FIX . "point(`id`,`day_1`) VALUES('$irs[id]','$point')");
            }
            echo "Tin nhan chinh xac.Ban khong phai la nguoi chien thang nhung ban van nhan duoc $point diem tu chuong trinh.";
            $mysql->disconnect();
            exit;
        }

        # Neu chua co ai WIN
        # Check Spam,neu thoi gian bat dau c.trinh den luc nhan SMS ma <20s ==> Spam = Tool

        if (($timenow - $timeBegin) <= 20) {
            echo "Spam";
            #$mysql->end();
            exit();
        }


        $qrs = $mysql->query("SELECT id FROM " . SpeedSMSConfig::$TB_FIX . "data WHERE phone='$phone'");
        $prs = $mysql->fetch_array($qrs);

        $mysql->query("UPDATE " . SpeedSMSConfig::$TB_FIX . "text SET WIN='$prs[id]'");
        echo "Xin chuc mung,ban la nguoi chien thang cuoc dua lan nay.Lien he SDT: 0982655098 de nhan thuong ban nhe";
        #$mysql->end();
        exit;
    }
    if ($timenow < $timeBegin) {
        echo "Xin loi,chuong trinh chua bat dau.Vui long cho " . ($timenow - $timeBegin) . " giay nua nhe!";
        exit;
    } else {
        echo "Chuong trinh da ket thuc.Cam on ban da tham gia chuong trinh!";
    }
} else {
    echo "Xin loi,noi dung tin nhan cua ban khong chinh xac voi noi dung chuong trinh.Vui long kiem tra lai.";
    $mysql->disconnect();
}
?><a>ádnvkjn</a>